package com.fzhucc.utils;


import javax.crypto.Cipher;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * RsaUtils
 *
 * @author hannannan
 * @date 2022/3/28 10:22
 * desc:
 */
public class RsaUtils2 {
    private static final String alg = "RSA";

    private static final String sign_alg = "SHA1WithRSA";

    /**
     * 加密
     *
     * @param key
     * @param content
     * @return
     */
    public static String encrypt(Key key, String content) {
        try {
            Cipher cipher = Cipher.getInstance(alg);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] re = cipher.doFinal(content.getBytes());
            return Base64.getEncoder().encodeToString(re);
        } catch (Exception e) {
            e.printStackTrace();
            return "rsa encrypt error";
        }
    }

    public static String encrypt(String key, String content) {
        PublicKey pubKey = getPublidKeyBybase64(key, alg);
        return encrypt(pubKey, content);
    }

    /**
     * 解密
     *
     * @param key
     * @param content
     * @return
     */
    public static String decrypt(Key key, String content) {
        try {
            Cipher cipher = Cipher.getInstance(alg);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] re = cipher.doFinal(Base64.getDecoder().decode(content));
            return new String(re);
        } catch (Exception e) {
            e.printStackTrace();
            return "rsa decrypt error";
        }
    }

    public static String sign(String key,String content){
        try {
            PrivateKey priKey = getPrivateKeyBybase64(key, alg);
            Signature instance = Signature.getInstance(sign_alg);
            instance.initSign(priKey);
            instance.update(content.getBytes("UTF-8"));
            byte[] sign = instance.sign();
            return Base64.getEncoder().encodeToString(sign);
        }catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException | SignatureException e) {
//            e.printStackTrace();
            return "rsa error";
        }
    }

    public static boolean verify(String key,String content,String sign){
        try {
            PublicKey pubKey = getPublidKeyBybase64(key, alg);
            Signature instance = Signature.getInstance(sign_alg);
            instance.initVerify(pubKey);
            instance.update(content.getBytes("UTF-8"));
            return instance.verify(Base64.getDecoder().decode(sign));
        } catch (Exception e) {
//            e.printStackTrace();
            return false;
        }
    }

    public static String decrypt(String key, String content) {
        Key priKey = getPrivateKeyBybase64(key, alg);
        return decrypt(priKey, content);
    }

    public static PublicKey getPublidKeyBybase64(String base64s, String alg) {
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64s));
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(alg);

            return keyFactory.generatePublic(keySpec);
        } catch (InvalidKeySpecException var4) {
            System.err.println("base64编码=" + base64s + "转"+alg+"公钥失败" + var4);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PrivateKey getPrivateKeyBybase64(String base64s, String alg) {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64s));
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(alg);
            return keyFactory.generatePrivate(keySpec);
        } catch (InvalidKeySpecException var4) {
            System.err.println("base64编码=" + base64s + "转RSA私钥失败" + var4);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
